package com.youi.web;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.entity.Purchasehistory;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.service.PurchasehistoryService;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Purchasehistory控制层
* @author Administrator
*/
@Controller
public class PurchasehistoryController  extends HttpRequestManage{

		@Autowired
		private PurchasehistoryService purchasehistoryService;

		/**
		 *添加或编辑Purchasehistory
		*/
		@ResponseBody
		@RequestMapping("addOrEditPurchasehistory")
		public String addOrEditPurchasehistory(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Purchasehistory purchasehistory=null;
			if (id!=null) {
				purchasehistory=purchasehistoryService.get(id);
			}else {
				purchasehistory=new Purchasehistory();
				purchasehistory.setDelStatus(DelStatus.NODEL);
				purchasehistory.setGmtCreated(new Date());
			}
			Long userId=optLong("userId",request);
			if (userId!=null){
				purchasehistory.setUserId(userId);
			}
			Long goodsId=optLong("goodsId",request);
			if (goodsId!=null){
				purchasehistory.setGoodsId(goodsId);
			}
			Integer count=optInteger("count",request);
			if (count!=null){
				purchasehistory.setCount(count);
			}
			purchasehistory.setGmtModify(new Date());
			purchasehistoryService.saveOrUpdate(purchasehistory);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除Purchasehistory
		*/
		@ResponseBody
		@RequestMapping("delPurchasehistoryByIds")
		public String delPurchasehistoryByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Purchasehistory> purchasehistoryList= purchasehistoryService.getByParams(params);
			if (purchasehistoryList==null||purchasehistoryList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Purchasehistory purchasehistory : purchasehistoryList) {
				purchasehistory.setDelStatus(DelStatus.DEL);
			}
			purchasehistoryService.saveOrUpdateAll(purchasehistoryList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * M-30
		 * 首页轮播购买记录
		 */
		@ResponseBody
		@RequestMapping("getPurchasehistory")
		public String getPurchasehistory(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
			/*
			Integer pageIndex = optInteger("pageIndex", request, 1);
			Integer pageNum = optInteger("pageNum", request, 10);
			String des = optString("des", request, Common.DESC);
			String pms = optString("pms", request);
			Date start = optDate("start", request, "yyyy-MM-dd HH:mm:ss");
			Date end = optDate("end", request, "yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams = new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram = new HashMap<String, Object>();
			sortPram.put(Common.ID, Common.DESC);
			Map<String, Object> searchPram = null;
			if (pms != null) {
				searchPram = new HashMap<String, Object>();
			}
			Map<String, Object> startTime = null;
			if (start != null) {
				startTime = new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED, start);
			}
			Map<String, Object> endTime = null;
			if (end != null) {
				endTime = new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED, end);
			}
			List<Purchasehistory> purchasehistoryList = purchasehistoryService.getByParamsList(prams, searchPram, sortPram, (pageIndex - 1) * pageNum, pageNum, startTime, endTime);
			if (purchasehistoryList == null || purchasehistoryList.size() == 0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			purchasehistoryService.getPurchasehistoryMsg(purchaseHistoryList);
			*/
			List<Purchasehistory> purchaseHistoryList = purchasehistoryService.listNoRepeatPurchaseUser(new HashMap<String, Object>(1) {{
				put("limit", 10);
			}});
			if (CollectionUtils.isEmpty(purchaseHistoryList)) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", purchaseHistoryList);
		}

		/**
		 *根据id查询Purchasehistory
		*/
		@ResponseBody
		@RequestMapping("getPurchasehistoryById")
		public String getPurchasehistoryById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Purchasehistory purchasehistory=purchasehistoryService.get(id);
			if (purchasehistory==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",purchasehistory);//jsonObject	json对象
		}

		/**
		 * 查询所有Purchasehistory
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllPurchasehistory")
		public String getAllPurchasehistory(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Purchasehistory> purchasehistoryList= purchasehistoryService.getByParams(null);
			if (purchasehistoryList==null||purchasehistoryList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",purchasehistoryList);//jsonObjectList	json对象集合
		}
}
